package com.qf.controller;

import com.qf.entity.Goods;
import com.qf.entity.PageBean;
import com.qf.service.GoodsService;
import com.qf.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wgy
 * @version V1.0
 * @project gp12_xiaomi
 * @package com.qf.controller
 * @description: ${TODO}
 * @date 2022/6/23 10:59
 */
@WebServlet(urlPatterns = "/goodsservlet")
public class GoodsServlet extends BaseServlet {

    private GoodsService goodsService;

    @Autowired
    public void setGoodsService(GoodsService goodsService) {
        this.goodsService = goodsService;
    }

    //1 分页查询
    public String getGoodsList(HttpServletRequest request, HttpServletResponse response) throws Exception{
        //1 接收数据
        //1.1接收分页数据
        String pageNum = request.getParameter("pageNum");
        String pageSize = request.getParameter("pageSize");
        //默认页面和页大小
        int pn=1;
        int ps=8;
        try {
            if(!StringUtils.isEmpty(pageNum)){
                pn= Integer.parseInt(pageNum);
                if(pn<1){
                    pn=1;
                }
            }
            if(!StringUtils.isEmpty(pageSize)){
                ps=Integer.parseInt(pageSize);
                if(ps<1){
                    ps=8;
                }
            }
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        //1.2 接收查询条件
        String typeId = request.getParameter("typeId");
        String name = request.getParameter("name");

        Map<Object,Object> map=new HashMap<>();
        map.put("pn",pn);
        map.put("ps",ps);
        if(!StringUtils.isEmpty(typeId)){
            map.put("typeId",typeId);
        }
        if(!StringUtils.isEmpty(name)){
           map.put("name",name);
        }


        try {
            PageBean<Goods> pageBean=goodsService.findByPage(map);
            List<Goods> data = pageBean.getData();
            for (Goods d : data) {
                System.out.println(d.toString());
            }
            //3放入域中
            request.setAttribute("pageBean", pageBean);
            request.setAttribute("typeId",typeId);
            request.setAttribute("name", name);
            //4转发
            return "/goodsList.jsp";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:/index.jsp";
    }
    //2 根据商品ID查询商品
    public String getGoodsById(HttpServletRequest request,HttpServletResponse response) throws Exception{
        //获取商品id
        String id = request.getParameter("id");
        //校验
        if(StringUtils.isEmpty(id)){
            return "redirect:/index.jsp";
        }

        //调用方法
        try {
            Goods goods=goodsService.findById(Integer.parseInt(id));
            request.setAttribute("goods",goods);
            return "/goodsDetail.jsp";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:/index.jsp";
    }
}
